<template>
  <div>
    <div class="header">
      <el-button type="primary" @click="back" icon="el-icon-arrow-left">返回</el-button>
      <p>修改密码</p>
      <span class="line"></span>
    </div>
    <section>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原始密码" prop="old">
          <el-input v-model="ruleForm.old"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button :plain="true" type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </section>

  </div>
</template>
  
<script>
export default {
  name: '',
  components: {},
  data() {
    var checkOld = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('原始密码不能为空'));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        old: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        old: [
          { validator: checkOld, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    back() {
      this.$router.push("/set/security")
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            message: '提交成功',
            type: 'success',
            duration: '1000',
          });
          this.$router.push("/set/security")
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
  
<style scoped lang='less'>
.header {
  position: relative;
  font-size: 28px;
  text-align: center;

  color: rgb(102, 102, 102);

  .el-button {
    padding-left: 12px;
    position: absolute;
    margin-left: -50%;
    background-color: rgb(225, 152, 163);
    border-color: rgb(109, 71, 71);
    color: rgb(236, 220, 220);
  }

  .line {
    display: block;
    width: 100%;
    height: 2px;
    background-color: silver;
    position: absolute;
    left: 0px;
    top: 50px;
  }
}

section {
  text-align: center;
  position: relative;
  margin-left: 20%;
  margin-top: 80px;
  width: 600px;
}
</style>